<template>
  <div class="child">
    <h3>子组件2</h3>
    <h3>电脑：{{ computer }}</h3>
    <h4>哥哥给的玩具:{{ toyer }}</h4>
  </div>
</template>

<script setup lang="ts" name="Child">
import { ref, onUnmounted } from "vue";
import emitter from "@/utlis/emitt";

let computer = ref("华为笔记本");
let toyer = ref("");
//1.绑定事件给哥哥玩具--发送请求事件
//2.接收事件回调
emitter.on("send-toy", (val: any) => {
  toyer.value = val;
});
//在组件卸载时解绑send-toy事件（优化内存）
onUnmounted(() => {
  emitter.off("send-toy"); //移除事件
});
</script>

<style scoped>
.child {
  background-color: skyblue;
  padding: 10px;
  border-radius: 10px;
  box-shadow: 0 0 10px black;
}
</style>
